package indexer;

import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import databaseconnector.DBProcessor;

public class Ask_index {

	//MySQL数据库连接器
	private static DBProcessor processor;
	
	//构造函数
	public Ask_index()
	{
		processor = new DBProcessor();
	}

	//从Ask数据库抽出数据建立索引
	public static void buildAskIndex(boolean create) throws IOException 
	{
		//索引输出目录
		Directory dir = FSDirectory.open(new File("E:/Index/AskIndex"));
		
		IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_40,
				new SmartChineseAnalyzer(Version.LUCENE_40));
		
		if (create) {
			cfg.setOpenMode(OpenMode.CREATE);
		} else {
			cfg.setOpenMode(OpenMode.CREATE_OR_APPEND);
		}
		
		IndexWriter writer = new IndexWriter(dir, cfg);
		ResultSet rlt = processor.getAsk();
		Document doc = new Document();
		
		
		//Field域构建
		Field id = new StoredField("id", Integer.MIN_VALUE);
		Field city = new TextField("city","",Field.Store.YES);
		Field region = new TextField("region", "", Field.Store.YES);
		Field xiaoqu = new TextField("xiaoqu", "", Field.Store.YES);
		Field min_price = new IntField("min_price",Integer.MIN_VALUE,Field.Store.YES);
		Field max_price = new IntField("max_price",Integer.MAX_VALUE,Field.Store.YES);
		Field room = new IntField("room",0,Field.Store.YES);
		Field hall = new IntField("hall",0,Field.Store.YES);
		Field toilet = new IntField("toilet",0,Field.Store.YES);
		Field person = new StoredField("person","");
		Field phone = new StoredField("phone","");
		Field detail = new TextField("detail","",Field.Store.YES);
		Field imglink = new StoredField("imglink", "");
		
		
		//将Field加入到doc中
		doc.add(id);
		doc.add(city);
		doc.add(region);
		doc.add(xiaoqu);
		doc.add(min_price);
		doc.add(max_price);
		doc.add(room);
		doc.add(hall);
		doc.add(toilet);
		doc.add(person);
		doc.add(phone);
		doc.add(detail);
		doc.add(imglink);
		

		//为doc的各Field赋值
		try {
			while (rlt.next()) {
				
				//设置Id值
				id.setIntValue(rlt.getInt(1));
				System.out.print(rlt.getInt(1) + " ");
				
				try{
					//设置City值
					String tempCity = rlt.getString(2).substring(0, 2).trim();
					if(tempCity != null)
					{
						city.setStringValue(tempCity);
						//System.out.print(tempCity + " ");
					}
					
					
					//设置Region值
					String tempRegion = rlt.getString(2).substring(3).trim();
					if(tempRegion != null){
						region.setStringValue(tempRegion);
						//System.out.print(tempRegion + " ");
					}
				}
				catch(Exception e){}
				
				//设置xiaoqu值
				String tempXiaoqu = rlt.getString(3);
				if(tempXiaoqu != null){
					xiaoqu.setStringValue(tempXiaoqu);
					//System.out.print(tempXiaoqu + " ");
				}
				
				//设置price值
				String tempPrice = rlt.getString(4);
				int tempMinPrice = Integer.MIN_VALUE;
				int tempMaxPrice = Integer.MAX_VALUE;
				try{
					if(tempPrice.contains("以下"))
					{
						tempMinPrice = Integer.parseInt(tempPrice.substring(0, tempPrice.indexOf("元")));
						max_price.setIntValue(tempMinPrice);
					}
					else if(tempPrice.contains("以上"))
					{
						tempMaxPrice = Integer.parseInt(tempPrice.substring(0, tempPrice.indexOf("元")));
						min_price.setIntValue(tempMaxPrice);	
					}
					else if(tempPrice.contains("-"))
					{
						tempMinPrice = Integer.parseInt(tempPrice.substring(0, tempPrice.indexOf("-")));
						tempMaxPrice = Integer.parseInt(tempPrice.substring(tempPrice.indexOf("-")+1, tempPrice.indexOf("元")));
						min_price.setIntValue(tempMinPrice);
						max_price.setIntValue(tempMaxPrice);
					}
					//System.out.print(tempMinPrice + " ");
					//System.out.print(tempMaxPrice + " ");
				}
				catch(Exception e){}
				
				//设置房间信息
				String tempHouse = rlt.getString(5);
				int tempRoom = 0;
				int tempHall = 0;
				int tempToilet = 0;
				if(tempHouse.contains("室"))
				{
					tempRoom = Integer.parseInt(tempHouse.substring(0, tempHouse.indexOf("室")));
					room.setIntValue(tempRoom);
					if(tempHouse.contains("厅"))
					{
						tempHall = Integer.parseInt(tempHouse.substring(tempHouse.indexOf("室")+1,
								tempHouse.indexOf("厅")));
						hall.setIntValue(tempHall);
						if(tempHouse.contains("卫"))
						{
							tempToilet = Integer.parseInt(tempHouse.substring(tempHouse.indexOf("厅")+1,
									tempHouse.indexOf("卫")));
							toilet.setIntValue(tempToilet);
						}
					}
				}
				//System.out.print(tempRoom + " " + tempHall + " " + tempToilet + " ");
				
				//设置person值
				String tempPerson = rlt.getString(6);
				if(tempPerson != null)
				{
					person.setStringValue(tempPerson);
					//System.out.print(tempPerson + " ");
				}
				
				//设置phone值
				String tempPhone = rlt.getString(7);
				if(tempPhone != null)
				{
					phone.setStringValue(tempPhone);
					//System.out.print(tempPhone + " ");
				}
				
				//设置detail值
				String tempDetail = rlt.getString(8);
				if(tempDetail != null)
				{
					detail.setStringValue(tempDetail);
					//System.out.print(tempDetail + " ");
				}
				
				//设置imglink值
				String tempImglink = rlt.getString(9);
				if(tempImglink != null){
					imglink.setStringValue(tempImglink);
					//System.out.print(tempDetail + " ");
				}
				
				System.out.println("");
				writer.addDocument(doc);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (create) {
			writer.forceMerge(1);
		}
		writer.close();
	}
}
